package com.zhang.controller.system;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.zhang.common.util.result.R;
import com.zhang.common.util.result.RUtils;

import com.zhang.system.entity.Menu;
import com.zhang.system.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 权限菜单表表 前端控制器
 * </p>
 *
 * @author 创世神
 * @since 2023-07-06
 */

@Controller
@ResponseBody
@RequestMapping("/permissions/menu/")
public class PermissionController {
    @Autowired
    IMenuService menuService;

    @PostMapping("addMenu")
    public R<String> addMenu(@RequestBody Menu menu) {
        Long addMenu = menuService.addMenu(menu);
        return RUtils.successMsg("菜单新增成功");
    }

    @GetMapping("getAllMenusToTree")
    public R getAllMenusToTree() {
        List<Menu> list = menuService.getAllMenusToTree();
        return RUtils.successData(list);
    }

    @GetMapping("getAllMenusByPage/{pageNum}/{pageSize}")
    public R getAllMenusByPageAndCondition(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @RequestParam Map searchMenuDto) {
        IPage list = menuService.getAllMenusByPageAndCondition(pageNum, pageSize, searchMenuDto);
        return RUtils.successData(list);
    }

    @GetMapping("getAllMenus")
    public R getAllMenus() {
        List<Menu> list = menuService.list();
        return RUtils.successData(list);
    }
    @SaCheckRole("admin")
    @GetMapping("directory/tree")
    public R getDirectoryPermissionByTree(){
       List<Menu> directory= menuService.getDirectoryPermissionByTree();
       return RUtils.successData(directory);
    }

    @PutMapping("updateMenu")
    public R updateMenu(@RequestBody Menu menu) {
        menuService.updateById(menu);
        return RUtils.successMsg("修改成功");
    }
    @DeleteMapping("{permissionId}")
    public R deletePermission(@PathVariable String permissionId){
        menuService.removeById(permissionId);
        return RUtils.successMsg("删除成功！");
    }
}
